# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'

from ... import pilelib as stack
from ...gamelib import info as gi
from ...packlib import info as cs
from ...piles import hexadeck as xstack
from .. import braid

GT, SL = gi.GT, gi.SL
Wrap = stack.StackWrapper


class MerlinsMeander(cs.HexA_Deck, braid.Braid):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.napoleon.BraidSmallLayout
        - row [4 over 2 cols E W braid, no offset]
        - reserve [1 N 1 S row E W braid, no offset]
        - talon [1 E foundation, no offset]
        - waste [1 E talon, no offset]
        - `s.trump` [1 N talon, no offset]
        - foundation [8 over 2 col E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon (max_rounds=3)
        Deal 1 card per waste on click. 2 redeal.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Foundation
        Init non trump base card. Build in rank by same suit wrap from
        base card. Dir determined by 2nd card on any pile. Play top. No trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.RowStack
        Init 1 card. Build any from any but reserve or braid. Play
        top. Max 1 card.

      _reserve_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Reserve
        Init 1 card. Play top. Gap deal 1 card from braid. Max 1 card.

      _braid_ : pysollib.stack.Stack
        .. pysollib.stack.OpenStack (max_cards=20)
        Init deal 20 open card. Build none. Play top.

      _trump_ : pysollib.stack.Stack
        .. pysollib.stacks.hexadeck..TrumpReserve
        Build any trump. Play top. Max 3 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=0x26404, name='Merlinʼs Meander',
                    category=cs.TYPE_HEXADECK, game_type=GT.NAPOLEON, decks=2,
                    redeals=2, skill_level=SL.BALANCED, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _trump_ = Wrap(xstack.TrumpReserve, max_cards=3)

    def _create_game(self, **kw):
        """
        _create_game([base=suits]) -> Layout

        Add a canvas info label for base_rank next to talon. Include trump
        reserve above talon.

        :returns: current layout with piles and their texts set.

        :seealso: `pysollib.layouts.napoleon` for keyword definitions.
        """
        kw.default('base', self['suits'])
        l, s = super(MerlinsMeander, super)._create_game(**kw), self.s
        s['trump'] = self._trump_(s['wastes'][-1].x + l.HXS, l.YM, game=self)
        l.DefaultGroups()
        self.sg['dropstacks'] += [s['trump']]
        self.sg['openstacks'] += [s['trump']]
        return l

    @property
    def is_complete(self):
        """
        Check all `s.foundations` are `is_filled`.
        """
        return all(s.is_filled for s in self.s['foundations'])


PROVIDES = [MerlinsMeander]